﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Controls;
using System.Windows;
using Bixas.Tangibilis.Controls.Primitives;
using System.Windows.Media;
using System.Diagnostics;
using System.Windows.Data;

namespace Bixas.Tangibilis.Controls
{
    [TemplatePart(Name = "PART_TouchThumb", Type = typeof(TouchThumb))]
    public class ScatterViewItem : TouchListBoxItem
    {
        private TranslateTransform _translateTransform = new TranslateTransform();
        private ScaleTransform _scaleTransform = new ScaleTransform();
        private RotateTransform _rotateTransform = new RotateTransform();

        static DependencyProperty RelativePositionProperty;

        static ScatterViewItem()
        {
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(ScatterViewItem), new FrameworkPropertyMetadata(typeof(ScatterViewItem)));
            ScatterViewItem.RelativePositionProperty = DependencyProperty.Register("Position", typeof(double), typeof(ScatterViewItem));      
        }

        public ScatterViewItem()
        {
            this.RenderTransformOrigin = new Point(0.5, 0.5);
        }

        private TouchThumb _Thumb;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this._Thumb = base.GetTemplateChild("PART_TouchThumb") as TouchThumb;
            this._Thumb.TouchRotateDelta += new TouchRotateDeltaEventHandler(_Thumb_TouchRotateDelta);
            this._Thumb.TouchScaleDelta += new TouchScaleDeltaEventHandler(_Thumb_TouchScaleDelta);
            this._Thumb.TouchDragDelta += new TouchDragDeltaEventHandler(_Thumb_TouchDragDelta);
            this._Thumb.DragDelta += new System.Windows.Controls.Primitives.DragDeltaEventHandler(_Thumb_DragDelta);
            this._rotateTransform.Angle = new Random().Next(360);
            this.UpdateTransform();
        }



        void _Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            this._translateTransform.X += e.HorizontalChange;
            this._translateTransform.Y += e.VerticalChange;
        }

        void _Thumb_TouchDragDelta(object sender, TouchDragDeltaEventArgs e)
        {
            this._translateTransform.X += e.HorizontalChange;
            this._translateTransform.Y += e.VerticalChange;
        }

        Binding positionBinding = new Binding("TranslateX");

        private void UpdateTransform()
        {
            TransformGroup group = new TransformGroup();
            group.Children.Add(_scaleTransform);
            
            group.Children.Add(_translateTransform);
            group.Children.Add(_rotateTransform);
             
             
            this.RenderTransform = group;
        }

        void _Thumb_TouchScaleDelta(object sender, TouchScaleDeltaEventArgs e)
        {
            this._scaleTransform.ScaleX += e.ScaleFactor;
            this._scaleTransform.ScaleY += e.ScaleFactor;

        }

        void _Thumb_TouchRotateDelta(object sender, TouchRotateDeltaEventArgs e)
        {
            this._rotateTransform.Angle += e.Angle;

        }


    }


}